<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cpsrule_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    // 新增
    function add($data = array()){
    	$this->db->insert('rsh_cps_rule', $data);
		return $this->db->insert_id();
    }
    
    // 保存
	function update($id, $data = array()){
		$this->db->where('id', $id);
		$this->db->update('rsh_cps_rule', $data);
		return $this->db->affected_rows();
	}
	
    // 启用,同一时间只允许单条规则
    function change_status($id){
    	$this->db->trans_strict(FALSE);
    	$this->db->trans_begin();
        $this->db->query("update rsh_cps_rule set fstatus = 1 where id = $id");
        $this->db->query("update rsh_cps_rule set fstatus = 0 where id != $id and fstatus != 3 ");
        if ($this->db->trans_status() === FALSE){
            $this->db->trans_rollback();
            return 0;
        }else {
            $this->db->trans_commit();
            return '1';
        }
    }
    
    // 获取
    function get($id){
    	$mkey = "cps_rule_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		$select = " select id,first_percent,second_percent,add_time,fstatus from rsh_cps_rule where fstatus != 3 id = {$id}";	
		$query = $this->db->query($select);
		$retsult = $query->result_array();
		return $retsult;
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
    }
    
    // 获取所有
    function getAll(){
		$select = " select id,first_percent,second_percent,add_time,fstatus from rsh_cps_rule where fstatus != 3";	
		$query = $this->db->query($select);
		$retsult = $query->result_array();
		return $retsult;
    }

    
    //TODO 每月 1 号 进行CPS资金清算
}